﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JohnnySins.Entities;
using Dapper;

namespace JohnnySins.DAL.Repositories
{
    public class RecentSceneRepository : BaseRepository<RecentScene>
    {
        public override IEnumerable<RecentScene> FindAll()
        {
            return Connection.Query<RecentScene>(@"SELECT rs.RecentSceneId, rs.RecentSceneTitle, rs.RecentSceneFileName, rs.IsActive, rs.Poster
                    FROM [RecentScene] (nolock) rs");
        }

        public IEnumerable<RecentScene> FindActive()
        {
            return Connection.Query<RecentScene>(@"SELECT rs.RecentSceneId, rs.RecentSceneTitle, rs.RecentSceneFileName, rs.IsActive, rs.Poster
                    FROM [RecentScene] (nolock) rs where rs.IsActive = 1 order by rs.RecentSceneId desc");
        }

        public override RecentScene Find(int id)
        {
            return Connection.Query<RecentScene>(
                @"select RecentSceneId, RecentSceneTitle, RecentSceneFileName, IsActive, Poster 
                from [RecentScene]
                where RecentSceneId = @RecentSceneId", new { RecentSceneId = id }).SingleOrDefault();
        }

        public override void Insert(RecentScene scene)
        {
            int result = Connection.Query<int>(
                @"INSERT INTO [RecentScene] (RecentSceneTitle, RecentSceneFileName, IsActive, Poster) 
                VALUES (@RecentSceneTitle, @RecentSceneFileName, @IsActive, @Poster); SELECT CAST(SCOPE_IDENTITY() AS INT);", scene).Single();
            scene.RecentSceneId = result;
        }

        public override void Update(RecentScene scene)
        {
            Connection.Execute(
                @"update [RecentScene] set RecentSceneTitle = @RecentSceneTitle, IsActive = @IsActive, Poster = @Poster, RecentSceneFileName = @RecentSceneFileName
                where RecentSceneId = @RecentSceneId", scene);
        }

        public void Activate(int id)
        {
            Connection.Execute(
                @"update [RecentScene] set IsActive = 1
                where RecentSceneId = @RecentSceneId", new { RecentSceneId = id });
        }

        public void Deactivate(int id)
        {
            Connection.Execute(
                @"update [RecentScene] set IsActive = 0
                where RecentSceneId = @RecentSceneId", new { RecentSceneId = id });
        }

        public override void Delete(int id)
        {
            Connection.Execute(
                @"delete from [RecentScene]
                    where RecentSceneId = @RecentSceneId", new { RecentSceneId = id });
        }
    }
}
